<template>
	<view class="mainContent">
		<view class="twoSecond">
			<view class="member">
				<text style="position: relative;">{{medicalTime || '选择体检时间：'}}
					<picker mode="date" :start="start" :end="end"  @change="bindDateChange" :disabled="status == 2" style="position: absolute;left:0;top: 0;width: 100%;height: 100%;"></picker> 
				</text>
			</view>
			<view class="save" v-if="status != 2" @click="seveAdd(1)">保存</view>
			<view class="toSend" v-if="status != 2"  @click="seveAdd(2)">确认存档</view>
		</view>
		<view class="uploadBox">
			<u-upload  ref="uUpload" :header="header" :form-data="{type:12}" :action="action" :file-list="imgUrls" :max-count="50" :uploadText="'选择图片<p>'+imgUrls.length+'/50</p>'" :deletable="status != 2" @on-uploaded="uploaded" @on-remove="remove"></u-upload>
		</view>
		<u-modal v-model="showModal" :show-cancel-button="true" @confirm="Modalconfirm" :content="showContent"></u-modal>
	</view>
</template>

<script>
	import http from '@/common/http/interface.js'
	export default {
		data (){
			const currentDate = this.getDate({
				format: true
			})
			return {
				start: '',
				end: currentDate,
				medicalTime:'',
				userName:'',
				imgUrls: [],
				action: http.config.baseUrl+'/File/multipleFiles',
				header:{
					"Token": localStorage.token
				},
				editId:'',
				memberdialog: false,
				showModal:false,
				showContent:'',
				medicalName:'',
				status:'', //存档状态
				userId: '',
			}
		},
		onLoad(options) {
			if(options.id){
				this.editId = options.id
				this.status = options.status
				if(options.status == 2){
					uni.setNavigationBarTitle({
						title: '查看体检报告'
					})
				}
				
				this.$api.MedicalfindbyId(options.id).then((res)=>{
					this.medicalName = res.data.result.medicalName
					this.medicalTime = res.data.result.medicalTime
					this.userId = res.data.result.userId
					this.userName = res.data.result.realName
					this.imgUrls = res.data.result.imgUrls.map((a)=>{
						return {url:a}
					})
				})
			}
			this.userId = localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).userId;
		},
		methods:{
			// 获取当前时间
			getDate(type) {
			   const date = new Date();
			   let year = date.getFullYear();
			   let month = date.getMonth() + 1;
			   let day = date.getDate();
			  
			   if (type === 'start') {
				year = year - 60;
			   } else if (type === 'end') {
				year = year + 2;
			   }
			   month = month > 9 ? month : '0' + month;;
			   day = day > 9 ? day : '0' + day;
			   return `${year}-${month}-${day}`;
			},
			// 日期
			bindDateChange(e) {  //日期选择
				this.medicalTime = e.target.value;
			},
			hidememberdialog(){
				this.memberdialog = false
			},
			//删除图片
			remove(index, lists, name){
			   this.imgUrls = lists
			},

			uploaded(lists, name){
				this.imgUrls = lists.map((a)=>{
					if(a.file){
						a.url = a.response.result[0]
					}
					return a
				})
			},
			submit(e){
				const _this = this;
				const imgUrls = _this.imgUrls.map((a)=>{
					if(a.response){
						return a.response.result[0]
					}else{
						return a.url
					}
				}) 
				const data = {
					id:_this.editId,
					medicalName: _this.medicalName,
					medicalTime: _this.medicalTime,
					imgUrls:imgUrls,
					status:e,
					type:1,
					userId: _this.userId
				}
				if(!_this.editId){
					_this.$api.Medicalinsert(data).then((res)=>{
						if(res.data.returnCode == 200){
							uni.showToast({title:'保存成功',icon:'none'})
							setTimeout(()=>{
								uni.navigateTo({
									url:'/pages/monitor/medicalReport/list'
								})
							},1000)
						}else{
							uni.showToast({title:res.data.message,icon:'none'})
						}
					})
				}else{
					_this.$api.Medicalupdate(data).then((res)=>{
						if(res.data.returnCode == 200){
							uni.showToast({title:'保存成功',icon:'none'})
							setTimeout(()=>{
								uni.navigateTo({
									url:'/pages/monitor/medicalReport/list'
								})
							},1000)
						}else{
							uni.showToast({title:res.data.message,icon:'none'})
						}
					})
				}
			},
			seveAdd(e){
				const _this = this;
				// if(!_this.medicalName){
				// 	uni.showToast({
				// 		title:'请填写体检机构！',
				// 		icon:'none'
				// 	})
				// 	return;
				// }
				if(!_this.medicalTime){
					uni.showToast({
						title:'请选择体检时间！',
						icon:'none'
					})
					return;
				}
				if(e==2){
					this.showModal = true
					this.showContent = '确认信息无误，存档后不可修改。'
					return;
				}
				this.submit(e)
			},
			Modalconfirm(){
				this.submit(2)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.mainContent {
		width: 750upx;
		min-height: calc(100vh - 44px);
		background-color: #FFFFFF;
		.twoSecond {
			height: 130upx;
			width: 750upx;
			display: flex;
			position: fixed;
			top: 44px;
			align-items: center;
			padding: 0 24upx;
			box-sizing: border-box;
			background: #05C6AD;
			z-index: 200;
			.member {
				flex: 1;
				margin-right: 45upx;
				height: 62upx;		
				border-radius: 6upx;
				line-height: 62upx;
				display: flex;
				image {
					width: 26upx;
					height: 26upx;
					margin: 0 18upx;
				}
				text {
					background-color: #FFFFFF;
					flex: 1;
					color: #05C6AD;
					font-size: 30upx;   
					border-radius: 8upx;
					padding-left: 24upx;
				}
				text:nth-child(2){
					 margin-left: 20upx;
				}
			}
			.save {
				width: 138upx;
				height: 62upx;
				line-height: 62upx;
				text-align: center;
				border-radius: 8upx;
				background: #BEFFF7;
				color: #333333;
				margin-right: 19upx;
				font-size: 28upx;
			}
			.toSend {
				background-color: #009383;
				width: 138upx;
				height: 62upx;
				line-height: 62upx;
				text-align: center;
				border-radius: 8upx;
				color: #FFFFFF;
				font-size: 28upx;
			}
		}
		.uploadBox {
			margin-top: 192upx;
			width: 750upx;
			padding: 0 60upx;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;
			.u-upload{
				width: 100%;
				display: flex;
				justify-content: space-between;
				/deep/ .u-list-item {
					width: 266upx !important;
					height: 350upx !important;
					background: #EFEFEF;
					border-radius: 10upx;
					position: relative;
				}
			}
		}
	}
	.flex{
		display: flex;
		font-size: 30rpx;
		.date{
			width: 334upx;
			height: 50upx;
			padding-left: 10upx;
			border: 1upx solid #E4E4E4;
			border-radius: 6upx;
		}
	}
</style>
